/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package codegener.comp;

import codegener.entity.ColumnInfo;
import codegener.tool.Gener;
import codegener.util.JsonUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author 32181
 */
public class TempForm extends javax.swing.JPanel {
   
    
    private String actionFTL=null;
    private String beanFTL=null;
    private String mapperFTL=null;
    private String serviceFTL=null;
    private String xmlMapperFTL=null;
    private String formFTL=null;
    private String listFTL=null;
    
    private List<JScrollPane>  scrollPanes=null;
    private List<JTextArea> areas=null;
    
    private Map<String,Object> var=new HashMap<>();
    /**
     * Creates new form tempForm
     */
    public TempForm() {
        initComponents();
        //readFTL();
        areas=new ArrayList<>();
        try{
            if(!Gener.getTemps().isEmpty()){
                Gener.getTemps().forEach((name,content)->{
                    JTextArea area=new JTextArea(content);
                    JScrollPane scrollPane=new JScrollPane(area);
                    rootTab.add(name, scrollPane);
                    area.setName(name);
                    areas.add(area);
                });
            }
           
        }catch(Exception e){
            e.printStackTrace();
        }
       initVar();
    }
    
    private void initVar(){
        var.put("priKey", "id");
        var.put("priKeyType", "long");
        var.put("priKeyColumn", "ID");
        
        var.put("tableName", "tab_user");
        var.put("BeanName", "TabUser");
        var.put("beanName", "tabUser");
        var.put("tabComment", "用户表");
        var.put("basePkg", "org.ithang.system");
        List<ColumnInfo> cols=new ArrayList<>();
        ColumnInfo idInfo=new ColumnInfo();
        idInfo.setPrimary(true);
        idInfo.setOrderNum(1);
        idInfo.setTableName("tab_user");
        idInfo.setDataType("NUMBER");
        idInfo.setMaxLength(25);
        idInfo.setJavaType("Long");
        idInfo.setComments("主键");
        idInfo.setName("ID");
        idInfo.setCname("id");
        cols.add(idInfo);
        
        ColumnInfo nameInfo=new ColumnInfo();
         nameInfo.setPrimary(false);
        nameInfo.setOrderNum(2);
        nameInfo.setName("UNAME");
        nameInfo.setTableName("tab_user");
        nameInfo.setDataType("VARCHAR2");
        nameInfo.setMaxLength(50);
        nameInfo.setJavaType("String");
        nameInfo.setComments("用户名称");
        nameInfo.setCname("uname");
        cols.add(nameInfo);
        
        var.put("fields",cols);
        var.put("beanFileName", "org.ithang.system.user.entity.User.java");
        var.put("mapperFileName", "org.ithang.system.user.mapper.UserMapper.java");
        var.put("xmlmapperFileName", "org.ithang.system.user.mapper.UserMapper.xml");
        var.put("serviceFileName", "org.ithang.system.user.service.UserService");
        var.put("actionFileName", "org.ithang.system.user.web.UserController");
        varTxt.setText(JsonUtils.toPrettyFormat(var));
    }
    
    public void saveFTL(){
        try{
            if(null!=areas&&!areas.isEmpty()){
                 Gener.getTemps().clear();
                 for(JTextArea area:areas){
                    if(null!=area.getName()){
                        Gener.getTemps().put(area.getName(), area.getText());
                        FileUtils.writeStringToFile(new File("./ftl/"+area.getName()), area.getText().trim(), "UTF-8");
                    }
                 }
            }
           
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
      
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        rootTab = new javax.swing.JTabbedPane();
        jScrollPane6 = new javax.swing.JScrollPane();
        varTxt = new javax.swing.JTextArea();

        setLayout(new java.awt.BorderLayout());

        rootTab.setMinimumSize(new java.awt.Dimension(800, 600));
        rootTab.setPreferredSize(new java.awt.Dimension(800, 600));

        varTxt.setEditable(false);
        varTxt.setColumns(20);
        varTxt.setRows(5);
        jScrollPane6.setViewportView(varTxt);

        rootTab.addTab("变量", jScrollPane6);

        add(rootTab, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JTabbedPane rootTab;
    private javax.swing.JTextArea varTxt;
    // End of variables declaration//GEN-END:variables
}
